package com.kanshu.ksgb.fastread.doudou.module.book.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig;
import com.kanshu.ksgb.fastread.doudou.common.business.ad.AdStyleBean;
import com.kanshu.ksgb.fastread.doudou.common.business.ad.AdUtils;
import com.kanshu.ksgb.fastread.doudou.common.business.ad.retrofit.ADConfigBean;
import com.kanshu.ksgb.fastread.doudou.common.util.ARouterUtils;
import com.kanshu.ksgb.fastread.doudou.common.business.utils.ReaderJumpConfig;
import com.kanshu.ksgb.fastread.doudou.common.imageloader.GlideImageLoader;
import com.kanshu.ksgb.fastread.doudou.common.view.CollapsedTextView;
import com.kanshu.ksgb.fastread.doudou.common.business.commonbean.BookInfo;
import com.kanshu.ksgb.fastread.doudou.module.book.bean.ChapterBean;
import com.kanshu.ksgb.fastread.doudou.common.business.commonbean.ReaderInputParams;
import com.kanshu.ksgb.fastread.doudou.common.business.utils.BookUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by wcy on 18/5/27
 */

public class AdBookDetailsHeaderLayout extends LinearLayout {
    @BindView(R.id.cover)
    ImageView mCover;
    @BindView(R.id.book_title)
    TextView mBookTitle;
    @BindView(R.id.label)
    TextView mLabel;
    @BindView(R.id.author)
    TextView mAuthor;
    @BindView(R.id.words)
    TextView mWords;
    @BindView(R.id.book_status)
    TextView mBookStatus;
    @BindView(R.id.book_desc)
    CollapsedTextView mBookDesc;
    @BindView(R.id.chapter_list)
    TextView mChapterList;
    @BindView(R.id.last_chapter_info)
    TextView mLastChapterInfo;
    @BindView(R.id.last_chapter_time)
    TextView mLastChapterTime;
    @BindView(R.id.ad_container)
    FrameLayout mAdContainer;
    BookInfo mBookInfo;
    ChapterBean mLastChapterBean;

    public AdBookDetailsHeaderLayout(Context context) {
        super(context);
        init();
    }

    public AdBookDetailsHeaderLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public AdBookDetailsHeaderLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.item_book_details_header_layout, this);
        ButterKnife.bind(view);
    }

    @OnClick({R.id.last_chapter_container, R.id.chapter_list})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.last_chapter_container:
                if (mLastChapterBean != null && mBookInfo != null) {
                    ReaderInputParams params = new ReaderInputParams();
                    params.book_id = mLastChapterBean.book_id;
                    params.book_title = mBookInfo.book_title;
                    params.content_id = mLastChapterBean.content_id;
                    params.order = mLastChapterBean.order;
                    ReaderJumpConfig.startReaderActivity(getContext(), params);
                }

                break;
            case R.id.chapter_list:
                if (mBookInfo != null) {
                    ARouterUtils.toActivity(ARouterConfig.BOOK_CHAPTER_LIST, "book_id", mBookInfo.book_id);
                }
                break;
        }
    }

    @SuppressLint("SetTextI18n")
    public void refreshByBookInfo(BookInfo item, ChapterBean lastChapter) {
        if (item != null) {
            GlideImageLoader.load(item.cover_url, mCover);
            mBookTitle.setText(item.book_title);
            mLabel.setText(BookUtils.getBookLabel(item));
            mAuthor.setText("作者:" + item.author_name);
            mWords.setText("共" + BookUtils.formatNum(item.word_count) + "字");
            mBookStatus.setText(BookUtils.getBookStatus(item));
            String ss = "内容简介:" + item.book_intro;
            SpannableString cs1 = new SpannableString(ss);
            int index1 = ss.indexOf("内容简介:");
            int len = "内容简介:".length();
            cs1.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.color666)), index1, index1 + len, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            mBookDesc.setText(cs1);
            mBookInfo = item;
            mBookDesc.setOnClickListener(v -> {
                mBookDesc.setIsResponseListener();
            });
        }
        if(lastChapter != null){
            mLastChapterBean = lastChapter;
            mLastChapterInfo.setText(lastChapter.title);
            String time = lastChapter.createtime + "更新";
            int index = time.indexOf("更新");
            SpannableString cs = new SpannableString(time);
            cs.setSpan(new ForegroundColorSpan(Color.parseColor("#F76678")), index, time.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            mLastChapterTime.setText(cs);
        }
        AdUtils.fetchAdUtil((Activity)getContext(), mAdContainer, null, ADConfigBean.POSITION_BOOK_DETAIL_SJXQ_1, AdStyleBean.BANNER_STYLE, 0, null);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }

    public void recycle() {
        AdUtils.destroyAd(mAdContainer);
    }
}
